﻿using clf.weighing.entity;
using HandyControl.Controls;
using MyHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Threading;
using UserControl = System.Windows.Controls.UserControl;
using Brushes = System.Windows.Media.Brushes;
using Color = System.Windows.Media.Color;
using Binding = System.Windows.Data.Binding;
using System.Collections.ObjectModel;

namespace weighing
{
    /// <summary>
    /// 称重数据的公共类
    /// </summary>
    public abstract class WeighingDataUserContent : UserControl
    {
        #region variables   
        protected WeighingBillVo? mWeighingBillVo;
        protected ObservableCollection<WeighingBillVo> mWeihingBillVos = new();
        protected DispatcherTimer? mTimer;
        protected WeightingBillType currBillType;
        #endregion


        #region abstract

        public abstract DataGrid GetDataGrid();

        #endregion

        public void ChangeDataColor(DataGridRowEventArgs eventArgs)
        {
            eventArgs.Row.Header = eventArgs.Row.GetIndex() + 1;
            eventArgs.Row.ToolTip = "双击查看详情";
            if (eventArgs.Row.DataContext is WeighingBillVo bill)
            {
                if (bill.Status == 0)
                {
                    eventArgs.Row.ToolTip = "双击进行出场过磅";
                    eventArgs.Row.Foreground = Brushes.Red;
                }
                else
                {
                    eventArgs.Row.Foreground = new SolidColorBrush(Color.FromRgb(66, 66, 66));
                }
                if (bill.UpGovernmentStatus == 1)
                {
                    if (bill.Status == 0)
                    {
                        eventArgs.Row.Foreground = new SolidColorBrush(Color.FromRgb(200, 00, 00));
                    }
                    else
                    {
                        eventArgs.Row.Foreground = Brushes.LightGreen;
                    }
                }
                else if (bill.UpGovernmentStatus == 2)
                {
                    eventArgs.Row.Foreground = Brushes.Green;
                }
            }
        }

        /// <summary>
        /// 动态设置备用字段的列
        /// </summary>
        protected void CreateBackupFieldUi(DataGrid dataGrid)
        {
            int type = (int)this.currBillType;

            List<BackupBillField> fields =BackupBillFieldDao.Instance.SelectAllByType(type);

            if (fields == null || fields.Count == 0) return;

            for (int i = 0; i < fields.Count; i++)
            {
                BackupBillField item = fields[i];
                if (item.isEnable == 1 && item.isShow == 1)
                {
                    DataGridTextColumn column = new DataGridTextColumn
                    {
                        Header = item.displayName,
                        Binding = new Binding(StringHelper.DBNamingToCamelCase(item.fiedName, true)),
                    };
                    InfoElement.SetTitle(column, item.fiedName);
                    int index = dataGrid.Columns.Count - 1;
                    dataGrid.Columns.Insert(index, column);
                }
            }
        }

        protected void UpdateBackupColumns(BackupBillField field)
        {
            if (field == null) return;
            if (field.isEnable == 1 && field.isShow == 1)
            {
                var column = FindColumn(field.fiedName);
                if (column == null)
                {
                    DataGridTextColumn newcolumn = new DataGridTextColumn
                    {
                        Header = field.displayName,
                        Binding = new Binding(StringHelper.DBNamingToCamelCase(field.fiedName, true)),                        
                    };
                    InfoElement.SetTitle(newcolumn, field.fiedName);
                    int index = GetDataGrid().Columns.Count - 1;
                    GetDataGrid().Columns.Insert(index, newcolumn);
                }
                else
                {
                    column.Header = field.displayName;
                    column.Binding = new Binding(StringHelper.DBNamingToCamelCase(field.fiedName, true));
                }
            }
            else
            {
                var column = FindColumn(field.fiedName);
                if (column != null)
                {
                    if (field.isEnable == 0 || field.isShow == 0)
                    {
                        GetDataGrid().Columns.Remove(column);
                    }
                }
            }
        }

        private DataGridTextColumn FindColumn(string fieldName)
        {
            var dg = GetDataGrid();

            if (dg == null) return null;

            DataGridTextColumn column = null;

            for (int i = dg.Columns.Count -1; i >= 0; i--)
            {
                var c = dg.Columns[i] as DataGridTextColumn;
                if (c != null)
                {
                
                    string fn = InfoElement.GetTitle(c);

                    if (!fn.IsNullOrEmpty())
                    {
                        if (fn.Equals(fieldName.Trim()))
                        {
                            column = c;
                            break;
                        }
                    }
                }
            }
            return column;
        }

        #region 列显示配制

        protected void ShowOrHideColumns()
        {
            var dg = GetDataGrid();
            string name = currBillType == WeightingBillType.Buy ? DataGridColumnsHelper.inputName: DataGridColumnsHelper.outputName;

            var dic = DataGridColumnsHelper.GetByName(name);

            var columns = dg.Columns;

            if(dic.Count == 0)
            {
                foreach (var c in columns)
                {
                    if (c is DataGridTextColumn tc)
                    {
                        string header = tc.Header.ToString();
                        if (dic.Keys.Contains(header))
                        {
                            dic[header] = true;
                        }
                        else
                        {
                            dic.Add(header, true);
                        }
                    }
                }

                DataGridColumnsHelper.Save(name, dic);
            }
            else
            {
                foreach (var c in columns)
                {
                    if (c is DataGridTextColumn tc)
                    {
                        string header = tc.Header.ToString();
                        if (dic.Keys.Contains(header))
                        {
                            if (dic[header] == true)
                            {
                                tc.Visibility = Visibility.Visible;
                            }
                            else
                            {
                                tc.Visibility = Visibility.Collapsed;
                            }
                        }
                        else
                        {
                            tc.Visibility = Visibility.Collapsed;
                        }
                    }
                }
            }
        }


        protected void SaveColumnSetting(string header,bool isShow)
        {          
            string name = currBillType == WeightingBillType.Buy ? DataGridColumnsHelper.inputName : DataGridColumnsHelper.outputName;

            DataGridColumnsHelper.Save(name, header, isShow);
        }

        #endregion

    }
}
